/*
 *  @ProductName@
 *  Copyright (C) @CopyrightYear@
 *    National Institute of Advanced Industrial Science and Technology (AIST)
 *    Registration Number: @AISTRegistrationNumber@
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package jp.go.aist.six.vuln.core.scap.nvd;

import org.exolab.castor.mapping.GeneralizedFieldHandler;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;



/**
 * A Castor FieldHandler for the NVD date/time.
 *
 * @author  Akihito Nakamura, AIST
 * @version $Id: VulnerabilityDatetimeHandler.java 9 2010-05-15 10:06:23Z nakamura5akihito $
 */
public class VulnerabilityDatetimeHandler
    extends GeneralizedFieldHandler
{

    /**
     * The date/time formatter and parser.
     */
    private SimpleDateFormat  _formatter;


    /**
     * The default date format pattern.
     */
    public static final String PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";



    /**
     * The time zone.
     */
    private TimeZone  _timeZone = TimeZone.getTimeZone( "GMT-5" );



    /**
     * Constructor.
     */
    public VulnerabilityDatetimeHandler()
    {
        this( PATTERN );
    }



    /**
     * Constructor.
     *
     * @param   pattern
     *  the date/time format pattern description.
     */
    public VulnerabilityDatetimeHandler(
                    final String pattern
                    )
    {
        _formatter = new SimpleDateFormat( pattern );
        _formatter.setTimeZone( _timeZone );
    }



    /**
     */
    public void setPattern(
                    final String pattern
                    )
    {
        _formatter = new SimpleDateFormat( pattern );
        _formatter.setTimeZone( _timeZone );
    }



    ////////////////////////////////////////////////////////////////
    //  extends GeneralizedFieldHandler
    ////////////////////////////////////////////////////////////////

    // from Date to String
    public Object convertUponGet(
                    Object value
                    )
    {
        if (value == null) {
            return null;
        }

        String  s = _formatter.format( (Date)value );
        // Inserts ':' in the time zone.
        //2010-01-29T13:30:00.947-0500 -> 2010-01-29T13:30:00.947-05:00
        int  index = s.length() - 2;
        s = s.substring( 0, index ) + ":00";
                                      // ":" + substring( index )

        return s;
    }



    // from String to Date
    //
    public Object convertUponSet(
                    Object value
                    )
    {
        Date  date = null;
        if (value != null) {
            String  svalue = (String)value;
            // Removes the last ':' in the time zone.
            //2010-01-29T13:30:00.947-05:00 -> 2010-01-29T13:30:00.947-0500
            int  index = svalue.lastIndexOf( ':' );
            if (index > 0) {
                svalue = svalue.substring( 0, index ) + "00";
                                                        // substring( index )
            }
            try {
                date = _formatter.parse( svalue );
            } catch (ParseException p_ex) {
                throw new IllegalArgumentException( p_ex.getMessage() );
            }
        }

        return date;
    }



    public Class<Date> getFieldType()
    {
        return Date.class;
    }



    public Object newInstance(
                    Object parent
                    )
        throws IllegalStateException
    {
        return null;
    }

}
// VulnerabilityDatetimeHandler

/* vim:set tabstop=4:set expandtab:set shiftwidth=4: */

